﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="menetlusinfoVastus", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(MenetlusinfoVastus))]
    public class MenetlusinfoVastusSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IMenetlusinfoVastus clazz;
        
        public MenetlusinfoVastusSerializer(IMenetlusinfoVastus clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("dokument");
            if (clazz.Dokument!=null) {
                Writer.WriteTypeAttribute("http://schemas.xmlsoap.org/soap/encoding/", "Array");
                Writer.WriteArrayTypeAttribute("menetlusinfo_dokument", "http://producers.arireg.xtee.riik.ee/producer/arireg", clazz.Dokument.Count);
                if (((clazz.Dokument == null) 
                            || (clazz.Dokument.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.Dokument != null) 
                            && (x < clazz.Dokument.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.Dokument[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaDokument = this.GetMetaData(clazz.Dokument[x]);
                        Writer.WriteTypeAttribute(metaDokument.TypeQualifiedName.Namespace, metaDokument.TypeQualifiedName.Name);
                        metaDokument.NewSerializer(clazz.Dokument[x]).WriteXml(Writer);
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Dokument");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="dokument") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>menetlusinfoVastus>dokument");
                }
                clazz.Dokument = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IMenetlusinfoDokument>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("dokument");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if ((Reader.IsElementNil() == false)) {
                                global::Xtee.Core.SerializationMetadata metaDokument = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IMenetlusinfoDokument>(Reader, "menetlusinfo_dokument", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                                global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IMenetlusinfoDokument item = metaDokument.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IMenetlusinfoDokument>();
                                clazz.Dokument.Add(item);
                                if ((Reader.IsEmptyElement == false)) {
                                    Reader.ReadStartElement("item");
                                    metaDokument.NewSerializer(item).ReadXml(Reader);
                                    Reader.ReadEndElement();
                                }
                                else {
                                    Reader.Skip();
                                }
                            }
                            else {
                                clazz.Dokument.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">item", Reader.LocalName);
                        }
                    }
                    if ((clazz.Dokument.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>menetlusinfoVastus>dokument", Reader.LocalName);
            }
        }
    }
}
